import requests
from bs4 import BeautifulSoup
import json
from my_fake_useragent import UserAgent
import time

ua = UserAgent()
headers = {'User-Agent':ua.random}

url_path = 'https://www.pexels.com/search/'
word = input('输入要下载的图片：')
url_tra = 'http://fanyi.youdao.com/translate?doctype=json&type=ZH_CN2EN&i={}'.format(word)
res = requests.get(url_tra)
json_data = json.loads(res.text)
en_word = json_data['translateResult'][0][0]['tgt']
url = url_path + en_word + '/'
start = time.time()
wb_data = requests.get(url,headers=headers)
soup = BeautifulSoup(wb_data.text,'lxml')
imgs= soup.select('article > a > img')
list = []
for img in imgs:
    photo = img.get('src')
    list.append(photo)
path = 'D://pexels/'
print(list)
for item in list:
    data = requests.get(item,headers=headers,stream=True)
    fp = open(path+item.split('?')[0][-10:],'wb')
    fp.write(data.content)
    fp.close()
end = time.time()
print('耗时：',end-start)
